﻿using Blis.Client;
using Blis.Common;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BlackSurvivalCheat
{
    static class HookCreatePacket
    {
        static bool maintoggle;
        static List<PatchHandler> handles;


        static HookCreatePacket()
        {
            maintoggle = true;

            handles = new List<PatchHandler>();
            handles.Add(new PatchHandler { Prefix = AccessTools.Method(typeof(HookCreatePacket), "GameClient_CreatePacket_Prefix", null, null), TargetType = typeof(GameClient), TargetMethonName = "CreatePacket" });
            handles.Add(new PatchHandler { Postfix = AccessTools.Method(typeof(HookCreatePacket), "GameClient_CreatePacket_Postfix", null, null), TargetType = typeof(GameClient), TargetMethonName = "CreatePacket" });
        }

        public static void Load(Harmony harmony)
        {
            foreach (PatchHandler handler in handles)
            {
                handler.Patch(harmony);
            }
        }

        public static void UnLoad(Harmony harmony)
        {
            foreach (PatchHandler handler in handles)
            {
                handler.Unpatch(harmony);
            }
        }

        static bool GameClient_CreatePacket_Prefix(ClientService __instance,ref byte[] __result,IPacket packet)
        {
            if (!maintoggle)
            {
                return true;
            }
            UnityEngine.Debug.Log("Send Packet:"+packet.GetType().Name);
            //LZ4MsgPackSerializer lz4 = new LZ4MsgPackSerializer();
            //__result=lz4.Serialize(packet);
            return true;
        }

        static void GameClient_CreatePacket_Postfix(ClientService __instance, ref byte[] __result, IPacket packet)
        {
            if (!maintoggle)
            {
                return;
            }

            string log = "";
            foreach(byte c in __result)
            {
                log += c.ToString("X2")+" ";
            }
            UnityEngine.Debug.Log(log);
        }
    }
}
